<template>
  <view style="flex-shrink: 0">
    <view :style="{height: searchBarTop + 'px',}">

    </view>
    <view
        v-if="showBar"
        class="search-bar-container"
        :style="{height:searchBarHeight + 'px'}">
      {{ header || '' }}
      <div class="back">
        <u-icon  v-if="isBack" name="arrow-left" size="32" @click="back"></u-icon>
      </div>
    </view>
  </view>
</template>
<script>
export default {
  name: "NavHeader",
  props: {
    header: {
      type: String,
      default: ''
    },
    isBack: {
      type: Boolean,
      default: false
    },
    showBar: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      searchBarTop: 0,   //搜索栏的外边框高度，单位px
      searchBarHeight: 0,  //搜索栏的高度，单位px
    }
  },
  mounted() {
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 获取胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();

    // 计算导航栏高度
    // 导航栏高度 = 胶囊高度 + (胶囊顶部距离状态栏底部的距离) * 2
    const navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - systemInfo.statusBarHeight) * 2;
    this.searchBarHeight = navBarHeight;
    this.searchBarTop =navBarHeight
  },
  methods: {
    back() {
      uni.navigateBack()
    }
  }
}
</script>
<style scoped lang="scss">

.search-bar-container {
  width: 100%;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: PingFang SC;
  font-weight: bold;
  font-size: 36rpx;
  color: #333333;
  position: relative;
}
.back {
  position: absolute;
  left: 24rpx;
  top: 50%;
  transform: translateY(-50%);
  width: 50rpx;
  height: 50rpx;
}
</style>
    position: absolute;
    left: 0;
    right: 0;
    font-family: PingFang SC;
    font-weight: bold;
    font-size: 36rpx;
    color: #333333;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

.back {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50rpx;
}
</style>
